package com.bite.demo.utils;

import org.junit.jupiter.api.Test;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtilsT {
    public static void main(String[] args) {
        //md5加密
        System.out.println(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //盐值
        System.out.println(UUID.randomUUID().toString().replace("-", ""));
    }
    @Test
    void encrpt() {
        String password = "123456";
        //生成盐值
        String salt  = UUID.randomUUID().toString().replace("-", "");
        //MD5（salt+password）
        String secretString = DigestUtils.md5DigestAsHex((salt+password).getBytes(StandardCharsets.UTF_8));
        //数据库存储 盐值＋密文
        String sqlPassword = salt+secretString;
        System.out.println(sqlPassword);
    }

    @Test
    void verify() {
         String inputPassword = "123456";
        String sqlPassword = "05c2f7dc766f4bafbb95692f12cb86a0ea1fe3a73ca18edd9063142e6c8ee982";

        String salt  = sqlPassword.substring(0,32);
        //MD5（salt+password）
        String secretString = DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes(StandardCharsets.UTF_8));

         Boolean result =(salt+secretString).equals(sqlPassword);
        System.out.println(result);
    }
}
